<?php
require_once 'Zend/Db/Table/Abstract.php';
class ExercisesModel extends Zend_Db_Table_Abstract
{
	protected $_name = 'exercises';
	protected $_id = 'id';
	
	/*
	 * Ham them bai tap cua 1 mon hoc
	*/
	public function add($exercises){
		$model = new ExercisesModel();
		$data = array('name'=>$exercises['name'],
				'time_start'=>$exercises['time_start'],
				'id_subjects'=>$exercises['id_subjects'],
				'link'=>$exercises['link'],
				'content'=>$exercises['content'],
				'time_end'=>$exercises['time_end']);
		$id = $model->insert($data);
		return $id;
	}

	/*
	 * Ham chinh sua bai tap cua 1 mon hoc
	*/
	public function edit($exercises){
		$model = new ExercisesModel();
		$data = array('name'=>$exercises['name'],
				'time_start'=>$exercises['time_start'],
				'id_subjects'=>$exercises['id_subjects'],
				'link'=>$exercises['link'],
				'content'=>$exercises['content'],
				'time_end'=>$exercises['time_end']);
		$model->update($data,"id=".$exercises["id"]);
		
	}
	
	/*
	 * Hàm upload file lên server.
	*/
	
	public function uploadFile(){
		if (!empty($_FILES)) {
			$realpath = dirname(__FILE__);
			$app = APPLICATION_PATH;
			//TREN SERVER : \yome-administrator\application
			$realpath = str_replace("\application" , "" , $app);
			$realpath = str_replace("\\" , "/" , $realpath);
			
			$year = date("Y");
			$month = date("m");
			
			$realpath = $realpath.'/public/uploads/'.$year."/".$month ;
			$path_db = '/uploads/'.$year."/".$month."/" ;
			
			$str = date("Y_m_d_H_i_s");
			$text = $str."_";
			move_uploaded_file($_FILES['file']["tmp_name"],"$realpath/".$text.$_FILES['file']["name"]);
			return $path_db.$text.$_FILES['file']["name"];
		}
	}
	
	/*
	 * Ham lay cac bai tap nop cua sv
	 */
	
	function getAllSubmitExercisesFromStudent($idExercises){
		$db = Zend_Registry::get('connectDB');
		$mysql = $db ->select()
					->from('submit_exercises',array('e_id' => 'submit_exercises.id_exercises' , 'link' => 'submit_exercises.link','time' => 'submit_exercises.time','content' => 'submit_exercises.content','title' => 'submit_exercises.title'))
				//	->join(array('e' => 'exercises'),'submit_exercises.id_exercises  = e.id',array('name_e' => 'e.name'))
					->join(array('s' => 'students'),'submit_exercises.id_students  = s.id',array('name_s' => 's.name','class_s' => 's.class'))
					->where('submit_exercises.id_exercises=?',$idExercises);
		$result = $db->fetchAll($mysql);
		return  $result;
	}
	
	/*
	* Ham lấy thông tin về môn học qua id
	*/

	function getExById($id){
		$db = Zend_Registry::get('connectDB');
		$mysql = $db ->select()
					->from('exercises')
					->join(array('s' => 'subjects'),'exercises.id_subjects  = s.id',array('name_s' => 's.name'))
					->where('exercises.id=?',$id);
		$result = $db->fetchAll($mysql);
		return  $result;
	}

	/*
	* Hàm trả về tên bài tập.
	*/
	function getNameExById($id){
		$db = Zend_Registry::get('connectDB');
		$mysql = $db ->select()
					->from('exercises')
					->where('id=?',$id);
		$result = $db->fetchAll($mysql);
		if(count($result)) return $result[0]["name"];
		else return "";
	}
    
    /*
    backcx
    Ham tra ve bai tap qua subject.id
    */
    function getDbBySubjectid($s_id){
        $db = Zend_Registry::get('connectDB');
		$mysql = $db ->select()
					->from('exercises')
                  	->where('id_subjects=?',$s_id);
		$result = $db->fetchAll($mysql);
 	    return  $result;
    }

	
}